/* ///////////////////////////////////////////////////////////////  */
/*  Program : sort.c                                                */
/* ---------------------------------------------------------------  */
/*  This is a set of sample C files for the UML diagram homework.   */
/*                                                                  */
/*  The program is written for the class "Introduction to Computer  */
/*  Science" by Hung-Cheng Su on 04/30/2012.                        */
/* //////////////////////////////////////////////////////////////// */
#include "main.h"
#include "sort.h"

void bubble_sort(List *mList)
{
    int  i, j;
    long *data = (long *) mList->data;

    for (i = mList->size - 1; i >= 0; i--)
    {
        for (j = 0; j < i; j++)
        {
            if ((*mList->cmp) ((data + j), (data + j + 1)) > 0)
                (*mList->swap) (data + j, data + j + 1);
        }
    }
}

int binary_search(List *mList, void *key, int imin, int imax)
{
    long *data = (long *) mList->data;

    if (imax < imin)
    {
        return -1;
    }
    else
    {
        int imid = (imin + imax) / 2;
        if ((*mList->cmp) ((data + imid), key) > 0)
        {
            return binary_search(mList, key, imin, imid - 1);
        }
        else if ((*mList->cmp) ((data + imid), key) < 0)
        {
            return binary_search(mList, key, imid + 1, imax);
        }
        else
        {
            return imid;
        }
    }
}
